////////////////////////////////////////////////////////////////////////////////

*************************CLEANING EWCS DATA*************************************

////////////////////////////////////////////////////////////////////////////////

cd "$europedata"

////////////////////////////////////////////////////////////////////////////////
*Clean data for regressions
////////////////////////////////////////////////////////////////////////////////

use "raw/ewcs_raw.dta", clear	
	rename y15_Q89g job_security
	gen insecure=1 if job_security==1|job_security==2
	recode insecure .=0
	drop if job_security!=1&job_security!=2&job_security!=3&job_security!=4&job_security!=5
	
*Temporary contract
gen temp = inrange(y15_Q11_lt,2,3)

*Self-employed
gen semp=(Y15_Q7_lt==2)

*Part-time
gen parttime=(y15_Q24<35)

*Male
gen male=(y15_Q2a==1)

*Age, age squared
gen age=y15_Q2b if y15_Q2b<100
gen age_squared=(age^2)/100

*Job tenure
gen jobtenure = y15_Q17 if y15_Q17<=75
replace jobtenure=0 if y15_Q17==999

*Industry code
gen industry=y15_nace_r1_17 if y15_nace_r1_17<=19

*University degree
gen uni = 1 if inrange(y15_ISCED_lt,5,6)
replace uni=0 if inrange(y15_ISCED_lt,0,4)

rename (w4 w5) (xw xw_europe)

*Sample variable
gen insample=!missing(insecure,temp,semp,parttime,male,age,age_squared,jobtenure,industry,uni)&age<=65

keep countid job_security insecure year temp semp parttime jobtenure age age_squared uni industry male insample xw

save "clean/ewcs_clean.dta", replace

*Insecurity by country
use "raw/ewcs_raw.dta", clear	
	rename y15_Q89g job_security
	gen insecure=1 if job_security==1|job_security==2
	recode insecure .=0
	drop if job_security!=1&job_security!=2&job_security!=3&job_security!=4&job_security!=5
	forval i=1/5 {
		gen j`i'=1 if job_security==`i'
		recode j`i' .=0
		}
	
	collapse (mean) j1 j2 j3 j4 j5 if job_security<=5 [pweight=w4], by(year countid)
	
	cap mkdir "data/temp"
save "temp/insecurity_by_country.dta", replace

*Now get Europe-wide average
use "raw/ewcs_raw.dta", clear
	rename y15_Q89g job_security
	forval i=1/5 {
		gen j`i'=1 if job_security==`i'
		recode j`i' .=0
		}
	
	collapse (mean) j1 j2 j3 j4 j5 if job_security<=5 [pweight=w5], by(year)
	gen countid=37
append using "temp/insecurity_by_country.dta"
	label values countid countid
	label define countid 37 "EU", add
	rename countid country

*Bin responses to see trends better
	gen disagree=j4+j5
	gen neutral=j3
	gen agree=j1+j2
	rename agree insecure
save "clean/insecurity_trends.dta", replace

////////////////////////////////////////////////////////////////////////////////

*European unemployment rates

////////////////////////////////////////////////////////////////////////////////

cd "$europedata"

import delimited "raw/unemp_eurostat.csv", clear

keep if unit=="Percentage of active population"
keep time geo value
rename (geo time) (id year)
replace value = "" if value == ":"
destring value, replace
rename value unemp
replace id = "Germany" if id == "Germany (until 1990 former territory of the FRG)"

la var year "Year"
la var id "Country"
la var unemp "Unemployment rate"

save "clean/unemp.dta", replace

cd "$europedata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}
